package com.yzw.phonestoreservice.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yzw.phonestoreservice.form.OrderForm;
import com.yzw.phonestoreservice.pojo.OrderMaster;
import com.yzw.phonestoreservice.pojo.PhoneInfo;
import com.yzw.phonestoreservice.pojo.PhoneSpecs;
import com.yzw.phonestoreservice.service.OrderMasterService;
import com.yzw.phonestoreservice.mapper.OrderMasterMapper;
import com.yzw.phonestoreservice.service.PhoneInfoService;
import com.yzw.phonestoreservice.service.PhoneSpecsService;
import com.yzw.phonestoreservice.vo.OrderMasterVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * @author YU
 * @description 针对表【order_master(订单表)】的数据库操作Service实现
 * @createDate 2022-11-07 09:59:25
 */
@Service
public class OrderMasterServiceImpl extends ServiceImpl<OrderMasterMapper, OrderMaster>
        implements OrderMasterService {

    @Autowired
    private PhoneSpecsService phoneSpecsService;

    @Autowired
    private PhoneInfoService phoneInfoService;

    @Autowired
    private OrderMasterService orderMasterService;
    @Override
    public String orderCreate(OrderForm orderForm) {
        OrderMaster orderMaster = new OrderMaster();
//        获得手机的规格
        PhoneSpecs specsbyId = phoneSpecsService.getById(orderForm.getSpecsId());

        PhoneInfo phoneInfoById = phoneInfoService.getById(specsbyId.getPhoneId());

        BeanUtils.copyProperties(phoneInfoById, orderMaster);

        BeanUtils.copyProperties(specsbyId, orderMaster);

        //把addressForm 中的数据取出来 封装到 BuyerAddress
        orderMaster.setBuyerName(orderForm.getName());
        orderMaster.setBuyerPhone(orderForm.getTel());
        orderMaster.setBuyerAddress(orderForm.getAddress());
        orderMaster.setSpecsId(orderForm.getSpecsId());
        orderMaster.setPhoneQuantity(orderForm.getQuantity());
        Integer phoneQuantity = orderMaster.getPhoneQuantity();
        BigDecimal specsPrice = specsbyId.getSpecsPrice();
        BigDecimal num = new BigDecimal(phoneQuantity);
        orderMaster.setOrderAmount(specsPrice.multiply(num));


        Boolean b = this.save(orderMaster);
        String orderId = orderMaster.getOrderId();

        return orderId;
    }

    @Override
    public OrderMasterVo orderDetail(String orderId) {
        OrderMaster orderMaster = this.getById(orderId);
        OrderMasterVo orderMasterVo = new OrderMasterVo();
        BeanUtils.copyProperties(orderMaster, orderMasterVo);
        BigDecimal specsPrice = orderMasterVo.getSpecsPrice();
        BigDecimal orderAmount = orderMasterVo.getOrderAmount();
        BigDecimal num = new BigDecimal(100);
        BigDecimal num1 = new BigDecimal(10);
        orderMasterVo.setFreight(num1);
        BigDecimal divide = specsPrice.divide(num);
        orderMasterVo.setSpecsPrice(divide);
        BigDecimal divide2 = orderAmount.divide(num).add(num1);
        orderMasterVo.setSpecsPrice(divide);
        orderMasterVo.setOrderAmount(divide2);

        return orderMasterVo;
    }

    @Override
    public Boolean orderPay(String orderId) {
        OrderMaster orderById = orderMasterService.getById(orderId);
        orderById.setPayStatus(1);
        orderMasterService.updateById(orderById);
        //手机版本信息id,库存-1
        Integer specsId = orderById.getSpecsId();
        PhoneSpecs phoneSpecs = phoneSpecsService.getById(specsId);
        phoneSpecs.setSpecsStock(phoneSpecs.getSpecsStock()-orderById.getPhoneQuantity());
        boolean b1 = phoneSpecsService.updateById(phoneSpecs);

        //获取手机信息，库存-1
        Integer phoneId = orderById.getPhoneId();
        PhoneInfo phoneById = phoneInfoService.getById(phoneId);
        phoneById.setPhoneStock(phoneById.getPhoneStock()-orderById.getPhoneQuantity());
        boolean b = phoneInfoService.updateById(phoneById);
        return b&&b1;
    }
}




